from django.db import models

# Create your models here.

from admins.models import RoleModel, AdminUserModel, Position

# 工作流模型
class Workflow(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, unique=True, null=True, blank=True, verbose_name='工作流名称')
    params = models.JSONField(null=True, blank=True, verbose_name='参数')
    status = models.IntegerField(choices=((1, '可用'), (0, '停用')), null=True, blank=True, verbose_name='状态')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'workflow'
        verbose_name_plural = '工作流'

# 工作流权限模型
class WorkflowPermission(models.Model):
    id = models.AutoField(primary_key=True)
    role_id = models.ForeignKey(RoleModel, on_delete=models.CASCADE, null=True, blank=True, verbose_name='角色')
    workflow_id = models.ForeignKey(Workflow, on_delete=models.CASCADE, null=True, blank=True, verbose_name='工作流')

    def __str__(self):
        return f"{self.role_id.name} - {self.workflow_id.name}"

    class Meta:
        verbose_name = 'permission'
        verbose_name_plural = '工作流权限'

# 工作流审批节点配置模型
class WorkflowApprovalNode(models.Model):
    id = models.AutoField(primary_key=True)
    workflow_id = models.ForeignKey(Workflow, on_delete=models.CASCADE, null=True, blank=True, verbose_name='工作流')
    position_id = models.ForeignKey(Position, on_delete=models.CASCADE, null=True, blank=True, verbose_name='职位')
    level = models.IntegerField(null=True, blank=True, verbose_name='步骤')

    def __str__(self):
        return f"{self.workflow_id.name} - Level {self.level}"

    class Meta:
        verbose_name = 'approvalNode'
        verbose_name_plural = '工作流审批节点配置'

# 任务模型
class Task(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=30, null=True, blank=True, verbose_name='任务名称')
    params = models.JSONField(null=True, blank=True, verbose_name='参数')
    workflow_id = models.ForeignKey(Workflow, on_delete=models.CASCADE, null=True, blank=True, verbose_name='工作流')
    userid = models.ForeignKey(AdminUserModel, on_delete=models.CASCADE, null=True, blank=True, verbose_name='用户')
    next_audit = models.ForeignKey(AdminUserModel, on_delete=models.SET_NULL, null=True, blank=True, related_name='next_audit_user', verbose_name='下一操作人')
    task_status = models.IntegerField(choices=((1, '新建'), (2, '审批中'), (3, '完成')), null=True, blank=True, verbose_name='任务状态')
    create_time = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name="创建时间")
    level = models.IntegerField(null=True, blank=True, verbose_name='级别')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'task'
        verbose_name_plural = '任务'

# 审批记录模型
class AuditRecord(models.Model):
    id = models.AutoField(primary_key=True)
    task_id = models.ForeignKey(Task, on_delete=models.CASCADE, null=True, blank=True, verbose_name='任务')
    audit_user = models.ForeignKey(AdminUserModel, on_delete=models.CASCADE, null=True, blank=True, verbose_name='操作人')
    audit_time = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name='操作时间')
    audit_status = models.IntegerField(null=True, blank=True, verbose_name='审核状态')

    def __str__(self):
        return f"{self.task_id.name} - {self.audit_user.name} - {self.audit_time}"

    class Meta:
        verbose_name = 'audit_record'
        verbose_name_plural = '审批记录'
